package com.zry.pda.presenter

import android.content.Intent
import android.util.Log
import android.view.View
import com.zry.pda.MainActivity
import com.zry.pda.R
import com.zry.pda.R.id.sectionNumberTV
import com.zry.pda.greendao.entity.PutStorageCode
import com.zry.pda.ui.activity.SectionNumberStorageActivity
import com.zry.pda.ui.activity.SubmitPutStorageActivity
import com.zry.pda.utils.DateTimeUtilsKt
import com.zry.pda.utils.NetWorkUtils
import com.zry.pda.utils.StringListUtil
import com.zry.pda.utils.ToastUtil
import kotlinx.android.synthetic.main.activity_section_number_storage.*

class SectionNumberStoragePresenter(private val activity: SectionNumberStorageActivity) {

    fun sectionNumberHandlerClick(view: View) {
        when (view.id) {
            R.id.submitConfirmSectionNumber -> {
                initCodeMethod()
            }
            R.id.sectionStartCodeImg -> {
                activity.sectionNumberStartCode.text = ""
            }
            R.id.sectionEndCodeImg -> {
                activity.sectionNumberEndCode.text = ""
            }
        }
    }

    private fun initCodeMethod() {
        val sectionStartCode = activity.sectionNumberStartCode.text.trim().toString()

        if (activity.title == "虚拟托盘") {
            if (sectionStartCode.isEmpty()) {
                activity.sectionNumberStartCode.text = ""
                return ToastUtil.toast(activity, "追溯码不能为空")
            }

            activity.outTreasuryCode!!.codes = sectionStartCode
            activity.outTreasuryCode!!.quantity = activity.sectionNumberTV.text.toString()
             activity.outTreasuryCodeDao!!.insert(activity.outTreasuryCode!!)
            setNextActivity(activity.title, activity.outTreasuryCode!!.timeID)

        } else {
            val sectionEndCode = activity.sectionNumberEndCode.text.trim().toString()
            if (sectionStartCode.isEmpty()) {
                activity.sectionNumberStartCode.text = ""
                return ToastUtil.toast(activity, "初始号段不能为空")
            }
            if (sectionEndCode.isEmpty()) {
                activity.sectionNumberEndCode.text = ""
                return ToastUtil.toast(activity, "结束号段不能为空")
            }
            val marginCode =
                StringListUtil().getStringIntercept(sectionEndCode, 6).toInt() - StringListUtil().getStringIntercept(sectionStartCode, 6).toInt()
            if (marginCode <= 0) {
                return ToastUtil.toast(activity, "初始号码必须小于结束号码")
            }

            when (activity.title) {
                "批量入库", "号段入库" -> {
                    val putStorageCode = getPutStorageCode()
                    if (StringListUtil().getStartStringIntercept(putStorageCode.minCode, 14) != StringListUtil().getStartStringIntercept(putStorageCode.maxCode, 14)) {
                        return ToastUtil.toast(activity, "初始码和结束码不是同一种产品")
                    }

                    activity.putStorageCodeDao!!.insert(putStorageCode)
                    setNextActivity(activity.title, putStorageCode.timeID)
                }
                "批量装箱", "号段装箱" -> {
                    activity.packingOperationInfo!!.submitMinCode = activity.longAndShortCodeList!![0].longCode
                    activity.packingOperationInfo!!.submitMaxCode = activity.longAndShortCodeList!![1].longCode

                    if (StringListUtil().getStartStringIntercept(activity.longAndShortCodeList!![0].longCode, 14) != StringListUtil().getStartStringIntercept(activity.longAndShortCodeList!![1].longCode, 14)) {
                        return ToastUtil.toast(activity, "初始码和结束码不是同一种产品")
                    }

                    if ((marginCode + 1) % activity.packingOperationInfo!!.packProportion.toInt() != 0) {
                        return ToastUtil.toast(activity, "输入的数量必须是装箱比例的整数倍")
                    }

                    val packingOperationInfo = activity.packingOperationInfo!!
                    activity.packingOperationInfoDao!!.insert(packingOperationInfo)
                    setNextActivity(activity.title, packingOperationInfo.timeID)
                }
                "批量出库", "号段出库" -> {

                    activity.outTreasuryCode!!.minCode = activity.longAndShortCodeList!![0].longCode
                    activity.outTreasuryCode!!.maxCode = activity.longAndShortCodeList!![1].longCode

                    if (StringListUtil().getStartStringIntercept(activity.longAndShortCodeList!![0].longCode, 14) != StringListUtil().getStartStringIntercept(activity.longAndShortCodeList!![1].longCode, 14)) {
                        return ToastUtil.toast(activity, "初始码和结束码不是同一种产品")
                    }

                    val outTreasuryCode = activity.outTreasuryCode!!
                    activity.outTreasuryCodeDao!!.insert(outTreasuryCode)

                    setNextActivity(activity.title, outTreasuryCode.timeID)
                }
            }
        }
    }


    private fun setNextActivity(title: String, TimeID: String) {
        if (!NetWorkUtils().isNetworkAvailable(activity)) {
            intentActivity()
        } else {
            val intent = Intent(activity, SubmitPutStorageActivity::class.java)
            intent.putExtra("title", title)
            intent.putExtra("TimeID", TimeID)
            activity.startActivity(intent)
        }
    }


    private fun intentActivity() {
        ToastUtil.toast(activity, "你没有连接网络，数据已经保存到本地")
        val intent = Intent(activity, MainActivity::class.java)
        activity.startActivity(intent)
    }

    private fun getPutStorageCode(): PutStorageCode {
        val putStorageCode =
            PutStorageCode(DateTimeUtilsKt().getNowDateTime("yyyyMMddHHmmss"), activity.storeOrderBean!!.StoreOrderID, activity.storeOrderBean?.StoreOrderCode, activity.storeOrderBean!!.ProductName, activity.storeOrderBean!!.NetUnitPackName, activity.storeOrderBean?.StoreType, "", activity.longAndShortCodeList!![1].longCode, activity.longAndShortCodeList!![0].longCode, activity.title)
        Log.e("TAG", putStorageCode.toString())
        return putStorageCode
    }
}